package com.sleep.satokendemo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import com.sleep.satokendemo.entry.R;
import com.sleep.satokendemo.entry.XMLDemo;
import io.github.linpeilie.annotations.AutoMapMapper;
import io.github.linpeilie.annotations.AutoMapping;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author 郭敖
 * @Date 2024年03月31日 19:18
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Cacheable(cacheNames = "userInfos",key = "#username")
    @PostMapping("/login")
    public R login(@RequestParam("username") String username, @RequestParam("password") String password){
        SaLoginModel model = new SaLoginModel();
        //给JWT设置额外的参数
        model.setExtra("password",password);
        StpUtil.login(username,model);
        StpUtil.getExtra("password");
        return R.success("登录成功",StpUtil.getTokenInfo());
    }

    @CachePut(cacheNames = "demos",key = "#demo.userName")
    @SaCheckPermission("user:add")
    @PostMapping("/add")
    public R add(@RequestBody XMLDemo demo){
        return R.success("添加成功");
    }

    @SaCheckPermission("user:test")
    @PostMapping("/test")
    public R test(){
        return R.success("测试成功");
    }
}
